/**
 * 
 */
package todomanager.theme;

import java.awt.Color;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;

import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.metal.DefaultMetalTheme;

/**
 * @author jonasbirgersson
 * 
 */
public class CustomTheme extends DefaultMetalTheme {
	
	private String name;

	private ColorUIResource primary1;
	private ColorUIResource primary2;
	private ColorUIResource primary3;

	private ColorUIResource secondary1;
	private ColorUIResource secondary2;
	private ColorUIResource secondary3;

	public CustomTheme() {

	}

	public CustomTheme(String fileName) {
		
		loadThemeFromResource(fileName);
	}

	/**
	 * Loads a theme from an ordinary file
	 * @param themeFile name of the the them file
	 * @throws IOException
	 */
	private void loadTheme(String themeFile) throws IOException {
		FileInputStream fis = new FileInputStream(themeFile);
		Properties themeProp = new Properties();
		themeProp.load(fis);
		
		name = themeProp.getProperty("name");

		primary1 = new ColorUIResource(new Color(
				parseColor(themeProp.getProperty("primary1"))));
		primary2 = new ColorUIResource(new Color(
				parseColor(themeProp.getProperty("primary2"))));
		primary3 = new ColorUIResource(new Color(
				parseColor(themeProp.getProperty("primary3"))));

		secondary1 = new ColorUIResource(new Color(
				parseColor(themeProp.getProperty("secondary1"))));
		secondary2 = new ColorUIResource(new Color(
				parseColor(themeProp.getProperty("secondary2"))));
		secondary3 = new ColorUIResource(new Color(
				parseColor(themeProp.getProperty("secondary3"))));
	}
	
	/**
	 * Loads a theme from a resource file
	 * @param resourceFile Name of the resource file
	 */
	private void loadThemeFromResource(String resourceFile) {
		
		try {
			ResourceBundle themeProp = ResourceBundle.getBundle("todomanager.theme." + resourceFile, Locale.ROOT);
			primary1 = new ColorUIResource(new Color(
					parseColor(themeProp.getString("primary1"))));
			primary2 = new ColorUIResource(new Color(
					parseColor(themeProp.getString("primary2"))));
			primary3 = new ColorUIResource(new Color(
					parseColor(themeProp.getString("primary3"))));
	
			secondary1 = new ColorUIResource(new Color(
					parseColor(themeProp.getString("secondary1"))));
			secondary2 = new ColorUIResource(new Color(
					parseColor(themeProp.getString("secondary2"))));
			secondary3 = new ColorUIResource(new Color(
					parseColor(themeProp.getString("secondary3"))));
		}
		catch (Exception e) {
			System.out.println(e);
		}
		
	}

	public String getName() {
		return name;
	}

	public int parseColor(String color) {
		// Parses the hexadecimal number represented as a string to an int.
		return Integer.parseInt(color, 16);

	}

	// the functions overridden from the base
	protected ColorUIResource getPrimary1() {
		return primary1;
	}

	protected ColorUIResource getPrimary2() {
		return primary2;
	}

	protected ColorUIResource getPrimary3() {
		return primary3;
	}

	protected ColorUIResource getSecondary1() {
		return secondary1;
	}

	protected ColorUIResource getSecondary2() {
		return secondary2;
	}

	protected ColorUIResource getSecondary3() {
		return secondary3;
	}

}
